package cc.yugu.yhdw.webserver.service.basic;

import cc.yugu.yhdw.common.constant.CacheKey;
import cc.yugu.yhdw.common.domain.basic.SmsConfig;
import cc.yugu.yhdw.common.tool.memcached.MemCachedConfig;
import com.whalin.MemCached.MemCachedClient;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
 * 短信配置业务切面类
 * 
 * @Date:Created in 2018/7/5 13:25
 * @Author:
 * @Modified By: zjn 2018/7/5 13:25
 */ 
@Component
@Aspect
public class SmsConfigServiceAspect {
    @Autowired
    MemCachedClient memCachedClient;

    @Pointcut("execution(* cc.yugu.yhdw.webserver.service.basic.SmsConfigService.findByAgent(..))")
    private void findByAgent() {}
    /**
    * 从缓存中查询
    * 
    * @Param: [joinPoint]
    * @Return: java.lang.Object
    * @Author By: zjn 2018/7/5  13:25
    */ 
    @Around("findByAgent()")
    public Object find(ProceedingJoinPoint joinPoint) throws Throwable {
        Integer id = (Integer) joinPoint.getArgs()[0];
        // 替换输出
        String key = CacheKey.key(CacheKey.K_AGENT_ID_V_SMS_CONFIG_LIST, id);
        // 根据key 从缓存客户端中获取缓存对象
        Object result = memCachedClient.get(key);
        if(result != null) {
            return result;
        }

        result = joinPoint.proceed();
        if(result != null) {
            memCachedClient.set(key, result, MemCachedConfig.CACHE_ONE_WEEK);
        }
        return result;
    }

    @Pointcut("execution(* cc.yugu.yhdw.webserver.service.basic.SmsConfigService.insert(..))")
    private void insert() {}
    /**
    * 插入
    * 
    * @Param: [joinPoint]
    * @Return: java.lang.Object
    * @Author By: zjn 2018/7/5  13:27
    */ 
    @Around("insert()")
    public Object insert(ProceedingJoinPoint joinPoint) throws Throwable {
        Object result = joinPoint.proceed();
        // 获取连接点方法运行时的入参列表(也就是传入目标方法的参数)；
        SmsConfig record = (SmsConfig) joinPoint.getArgs()[0];
        // 根据运营商id删除
        memCachedClient.delete(CacheKey.key(CacheKey.K_AGENT_ID_V_SMS_ACCOUNT_LIST, record.getAgentId()));
        memCachedClient.delete(CacheKey.key(CacheKey.K_AGENT_ID_V_SMS_CONFIG_LIST, record.getAgentId()));
        return result;
    }

    @Pointcut("execution(* cc.yugu.yhdw.webserver.service.basic.SmsConfigService.update(..))")
    private void update() {}
    /**
    * 更新
    * 
    * @Param: [joinPoint]
    * @Return: java.lang.Object
    * @Author By: zjn 2018/7/5  13:37
    */ 
    @Around("update()")
    public Object update(ProceedingJoinPoint joinPoint) throws Throwable {
        Object result = joinPoint.proceed();
        SmsConfig record = (SmsConfig) joinPoint.getArgs()[0];
        memCachedClient.delete(CacheKey.key(CacheKey.K_ID_V_SMS_CONFIG_INFO, record.getId()));
        memCachedClient.delete(CacheKey.key(CacheKey.K_AGENT_ID_V_SMS_ACCOUNT_LIST, record.getAgentId()));
        memCachedClient.delete(CacheKey.key(CacheKey.K_AGENT_ID_V_SMS_CONFIG_LIST, record.getAgentId()));
        return result;
    }
}
